package com.vibeplayer.app.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.vibeplayer.app.R;
import com.vibeplayer.app.util.LrcParser;

import java.util.List;

public class LyricLineAdapter extends RecyclerView.Adapter<LyricLineAdapter.ViewHolder> {

    private List<LrcParser.LrcEntry> lyricLines;
    private int highlightedPosition = -1;
    
    public LyricLineAdapter(List<LrcParser.LrcEntry> lyricLines) {
        this.lyricLines = lyricLines;
    }
    
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_lyric_line, parent, false);
        return new ViewHolder(view);
    }
    
    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        LrcParser.LrcEntry entry = lyricLines.get(position);
        
        // 格式化时间戳
        String timestamp = formatTimestamp(entry.time);
        holder.txtTimestamp.setText(timestamp);
        
        // 设置歌词文本
        holder.txtLyricLine.setText(entry.text);
        
        // 设置高亮状态
        if (position == highlightedPosition) {
            holder.txtTimestamp.setTextColor(holder.itemView.getContext().getResources().getColor(R.color.colorAccent));
            holder.txtLyricLine.setTextColor(holder.itemView.getContext().getResources().getColor(R.color.colorAccent));
        } else {
            holder.txtTimestamp.setTextColor(holder.itemView.getContext().getResources().getColor(R.color.gray));
            holder.txtLyricLine.setTextColor(holder.itemView.getContext().getResources().getColor(android.R.color.black));
        }
    }
    
    @Override
    public int getItemCount() {
        return lyricLines.size();
    }
    
    /**
     * 设置歌词行列表
     * @param lyricLines 歌词行列表
     */
    public void setLyricLines(List<LrcParser.LrcEntry> lyricLines) {
        this.lyricLines = lyricLines;
        notifyDataSetChanged();
    }
    
    /**
     * 设置高亮位置
     * @param position 高亮位置
     */
    public void setHighlightedPosition(int position) {
        int oldPosition = highlightedPosition;
        highlightedPosition = position;
        
        if (oldPosition >= 0) {
            notifyItemChanged(oldPosition);
        }
        if (highlightedPosition >= 0) {
            notifyItemChanged(highlightedPosition);
        }
    }
    
    /**
     * 格式化时间戳
     * @param time 时间（毫秒）
     * @return 格式化的时间戳
     */
    private String formatTimestamp(long time) {
        long minutes = time / 60000;
        long seconds = (time % 60000) / 1000;
        long milliseconds = (time % 1000) / 10;
        return String.format("[%02d:%02d.%02d]", minutes, seconds, milliseconds);
    }
    
    /**
     * ViewHolder类
     */
    public static class ViewHolder extends RecyclerView.ViewHolder {
        TextView txtTimestamp;
        TextView txtLyricLine;
        
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            txtTimestamp = itemView.findViewById(R.id.txtTimestamp);
            txtLyricLine = itemView.findViewById(R.id.txtLyricLine);
        }
    }
} 